<?php
$title = '一键广播';
/** @var array $groups */
/** @var array|null $results */
/** @var string|null $error */
require __DIR__ . '/../partials/head.php';
require __DIR__ . '/../partials/nav.php';
?>
<section class="section">
    <div class="app-container" style="display:flex;flex-direction:column;gap:24px;">
        <?php if ($error): ?>
            <div class="card" style="background:rgba(255,59,48,0.12);border:1px solid rgba(255,59,48,0.3);color:#d32f2f;">
                <?= htmlspecialchars($error) ?>
            </div>
        <?php endif; ?>
        <div class="card" style="gap:18px;">
            <h2 style="margin:0;">广播参数</h2>
            <form method="post" action="/admin/broadcast" style="display:grid;gap:18px;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));">
                <div class="field">
                    <label>选择分组</label>
                    <select name="group_id" required>
                        <?php foreach ($groups as $group): ?>
                            <option value="<?= htmlspecialchars((string)$group['id']) ?>"><?= htmlspecialchars($group['name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="field">
                    <label>Connector</label>
                    <input type="text" name="connector" value="gate_io_perpetual" required>
                </div>
                <div class="field">
                    <label>交易对</label>
                    <input type="text" name="symbol" value="ETH-USDT" required>
                </div>
                <div class="field">
                    <label>方向</label>
                    <select name="trade_type">
                        <option value="BUY">BUY</option>
                        <option value="SELL">SELL</option>
                    </select>
                </div>
                <div class="field">
                    <label>投入百分比（%）</label>
                    <input type="number" name="invest_percent" value="10" min="0.1" max="100" step="0.1" required>
                </div>
                <div class="field">
                    <label>订单类型</label>
                    <select name="order_type">
                        <option value="MARKET">MARKET</option>
                        <option value="LIMIT">LIMIT</option>
                    </select>
                </div>
                <div class="field">
                    <label>价格（限价单可选）</label>
                    <input type="number" name="price" step="0.1">
                </div>
                <div class="field">
                    <label>动作</label>
                    <select name="position_action">
                        <option value="OPEN">OPEN</option>
                        <option value="CLOSE">CLOSE</option>
                    </select>
                </div>
                <div style="grid-column:1 / -1;display:flex;justify-content:flex-end;">
                    <button class="btn" type="submit">广播下单</button>
                </div>
            </form>
        </div>
        <div class="card" style="gap:18px;">
            <h3 style="margin:0;">分组详情</h3>
            <?php foreach ($groups as $group): ?>
                <div style="padding:16px;border-radius:18px;background:rgba(0,0,0,0.03);margin-bottom:16px;">
                    <strong><?= htmlspecialchars($group['name']) ?></strong>
                    <p style="color:var(--text-secondary);margin:6px 0;">成员：</p>
                    <ul style="margin:0;padding-left:18px;">
                        <?php foreach ($group['members'] as $member): ?>
                            <li><?= htmlspecialchars($member['account_name']) ?> · 权重(预留) <?= htmlspecialchars((string)$member['weight']) ?> · <?= $member['enabled'] ? '启用' : '禁用' ?></li>
                        <?php endforeach; ?>
                        <?php if (empty($group['members'])): ?>
                            <li>暂无成员</li>
                        <?php endif; ?>
                    </ul>
                </div>
            <?php endforeach; ?>
        </div>
        <?php if ($results): ?>
            <div class="card" style="gap:16px;">
                <h3 style="margin:0;">广播回执</h3>
                <table class="table">
                    <thead>
                    <tr>
                        <th>账户</th>
                        <th>状态</th>
                        <th>错误</th>
                        <th>Tx Ref</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($results as $row): ?>
                        <tr>
                            <td><?= htmlspecialchars($row['account_name']) ?></td>
                            <td><?= htmlspecialchars($row['status']) ?></td>
                            <td><?= htmlspecialchars((string)($row['error'] ?? '-')) ?></td>
                            <td><?= htmlspecialchars((string)($row['tx_ref'] ?? '-')) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</section>
<?php require __DIR__ . '/../partials/footer.php'; ?>
